//引入
var express = require('express')
var fs = require('fs')
var path = require('path')
var app = express()
//设置中间件
app.use(express.static('static'))
// 设置post请求参数的中间件
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
// 读取yonghu.json中的数据
var filepath = path.join(__dirname, 'static/yonghu.json')
var yonghuarr = fs.readFileSync(filepath)
yonghuarr = yonghuarr.toString() ? JSON.parse(yonghuarr) : []

// 注册
app.post('/api/register', (req, res) => {
    var { username, password } = req.body
    console.log(username, password);
    var i = null
    i = yonghuarr.findIndex((item) => {
        return username == item.username
    })
    console.log(i);
    if (i == -1) {
        yonghuarr.push({ "username": username, "password": password })
        fs.writeFileSync(filepath, JSON.stringify(yonghuarr), 'utf-8')
        res.send({ code: 200, msg: '注册成功' })
    }
    else {
        res.send({ code: 400, msg: '该账号已被注册' })
    }
})

// 登录
app.post('/api/login', (req, res) => {
    var { username, password } = req.body
    var i
    i = yonghuarr.findIndex((item) => {
        return item.username == username && item.password == password
    })
    console.log(i);
    if (i == -1) {
        res.send({ code: 400, msg: '登录失败，密码或用户名不正确' })
    }
    else {
        res.send({ code: 200, msg: '登录成功' })
    }
})

// 获取数据  分页
app.get('/api/list', (req, res) => {
    var { page, count } = req.query;

    var newarr = gangweiarr.slice((page - 1) * count, page * count)
    // 返回的数据
    res.send({ code: 200, msg: '请求成功', list: newarr })
    // console.log(page);
    // 得到的参数
    // console.log(req.query);
})

var gangweiarr = [
    { bianhao: '1', num: 'ceo', name: '董事长', paixu: '1', zhuangtai: '正常', time: '17:00' },
    { bianhao: '2', num: 'eo', name: '项目经理', paixu: '2', zhuangtai: '正常', time: '17:00' },
    { bianhao: '3', num: 'se', name: '董事长', paixu: '3', zhuangtai: '正常', time: '17:00' },
    { bianhao: '4', num: 'co', name: '项目经理', paixu: '4', zhuangtai: '正常', time: '17:00' },
    { bianhao: '5', num: 'hr', name: '董事长', paixu: '5', zhuangtai: '正常', time: '17:00' },
    { bianhao: '6', num: 'ceo', name: '项目经理', paixu: '6', zhuangtai: '正常', time: '17:00' },
]


//新增数据 
app.post('/api/add', (req, res) => {
    var { name, num } = req.body
    gangweiarr.push({ "bianhao": "1", "num": num, "name": name, "paixu": "1", "zhuangtai": "正常", "time": "17:00" })
    res.send({ code: 200, msg: '添加成功', list: gangweiarr })
})


// 删除
app.post('/api/delete', (req, res) => {
    var { bianhao } = req.body
    console.log(bianhao);
    var i = gangweiarr.findIndex((item) => {
        return item.bianhao == bianhao
    })
    gangweiarr.splice(i, 1)
    res.send({ code: 200, msg: '删除成功', list: gangweiarr })

})

// 模糊查询
app.get('/api/search', (req, res) => {
    var { searchKey } = req.query
    var gangweiarr1 = gangweiarr.filter((item) => {
        return item.num.includes(searchKey)
    })
    console.log(gangweiarr1);
    res.send({ code: 200, msg: "查询成功", list: gangweiarr1 })
})


app.listen(2020, () => {
    console.log('服务器运行中');
})